package com.interview.javabasic.DataStructrue;/*
@李子宁
Happy,happy everyday!
冲鸭！
*/

public class Josephu {
    public static void main(String[] args) {
        //测试看看构建环形链表和构建是否ok
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();

        //测试出圈
        circleSingleLinkedList.count(1,2,5);
    }


}

class CircleSingleLinkedList{
    //创建一个first节点，当前没有编号
    private Boy first = new Boy(-1);
    //添加小孩节点构建成一个环形链表。
    public void addBoy(int nums){
        //nums做一个数据验证
        if (nums<1){
            System.out.println("nums的值不正确");
            return;
        }
        Boy cur = null;//帮助构建环形链表
        //使用for循环创建环形链表
        for (int i = 1; i <= nums; i++) {
            //根据编号创建小孩节点
            Boy boy = new Boy(i);
            //如果是第一个小孩，就是第一个换
            if (i==1){
                first = boy;
                first.setNext(first);
                cur = first;//让cur来动起来
            }else{
                cur.setNext(boy);
                boy.setNext(first);
                cur = boy;
            }
        }
    }

    //遍历当前的环形链表
    public void showBoy(){
        //判断是否为空
        if (first==null){
            System.out.println("链表为空，没有小孩");
            return;
        }
        //因为first不能动
        Boy cur = first;
        while (true){
            System.out.printf("小孩的编号%d\n",cur.getNo());
            if (cur.getNext()==first) {
                //说明遍历完毕
                break;
            }
            cur = cur.getNext();
        }
    }

    //根据顺序出圈
    public void count(int startNo,int countNum,int nums){
        //先对数据校验
        if (first==null || startNo <1 || startNo > nums){
            System.out.println("数据不合法。");
            return;
        }
        //创建一个辅助指针，帮助小孩出圈
        Boy helper = first;
        while (true){
            if (helper.getNext()==first){
                //指向最后了
                break;
            }
            helper = helper.getNext();
        }
        //报数前
        for (int i = 0; i < startNo - 1; i++) {
            first = first.getNext();
            helper = helper.getNext();
        }
        //报数时，这里是一个循环
        while (true){
            if (helper==first){
                //说明圈中只有一个孩子
                break;
            }
            //开始移动
            for (int i = 0; i < countNum - 1; i++) {
                first = first.getNext();
                helper = helper.getNext();
            }
            //这时first指向的节点就是小孩要出圈的节点
            System.out.printf("小孩%d出圈\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩的编号%d\n",first.getNo());
    }
}

//定义一个节点
class Boy{
    private int no;
    private Boy next;
    public Boy(int no){
        this.no = no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}
